package com.ncmx.config;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import com.ncmx.lmax.jms.*;

@Configuration
@PropertySource("classpath:jmsconfigs.properties")
public class JmsConfig {
	
	@Value("${ncmx.exchange.jms.brokerUrl}")
	private String jmsBrokerURL;
	
	 @Bean
	 public JmsTemplate jmsTemplate() {
	 JmsTemplate jmsTemplate = new JmsTemplate();
	 jmsTemplate.setConnectionFactory(connectionFactory());
	 return jmsTemplate;
	 }
	  
	 @Bean
	 public ActiveMQConnectionFactory connectionFactory() {
	 ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
	 System.out.println(jmsBrokerURL);
	 activeMQConnectionFactory.setBrokerURL(jmsBrokerURL);
	 return activeMQConnectionFactory;
	 }
	  
	 @Bean
	 public JmsSender jmsSender() {
	 JmsSender jmsSender = new JmsSender();
	 jmsTemplate().setDefaultDestination(new ActiveMQQueue("ncmx.order.queue"));
	 jmsSender.setJmsTemplate(jmsTemplate());
	 return jmsSender;
	 }
	 
	 @Bean
	 public DefaultMessageListenerContainer orderMessageListenerContainer() {
	 DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
	 messageListenerContainer.setConnectionFactory(connectionFactory());
	 messageListenerContainer.setDestinationName("ncmx.order.queue");
	 messageListenerContainer.setMessageListener(jmsReceiver());
	 return messageListenerContainer;
	 }
	
	
	 
	 @Bean
	 public JmsReceiver jmsReceiver() {
	 return new JmsReceiver();
	 }
	
	 @Bean
	  public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
	    return new PropertySourcesPlaceholderConfigurer();
	  }
	 
	 @Bean
		public MockOrders mockOrder(){
			return new MockOrders();
		}

}
